package com.gopay.common.util.xml.utils.validator;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import com.gopay.common.util.xml.xmlutils.message.Validator;

/**
 * 正则表达式校验
 * @author liujy
 *
 */
public class DefaultPatternValidator extends BaseValidator {
    @Override
    protected boolean validator(Object validator_, Object value) {
        if (null == value || "".equals(value)) {
            return true;
        }
        Validator validator = (Validator) validator_;
        if (StringUtils.isNotBlank(validator.getPattern())) {
            Pattern pattern = Pattern.compile(validator.getPattern());
            Matcher matcher = pattern.matcher(value.toString());
            boolean flag = matcher.find();
            if (!flag) {
                return false;
            }
        }
        return true;
    }
}
